﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;

namespace BullShitPoker
{
    public partial class StartScreen : Form
    {
        public StartScreen()
        {
            InitializeComponent();
        }

        private void Ok_Click(object sender, EventArgs e)
        {
            if (serverBox.Text.Length == 0 || nameBox.Text.Length == 0) return;
            string response = "";
            if (!Client.init(serverBox.Text, nameBox.Text))
            {
                MessageBox.Show("Server doesn't response. Please check the address of the server",
                    "Server doesn't response", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
               response = Client.Sender("a\nPing");
            }
            catch
            {
                MessageBox.Show("Server doesn't response. Please check the address of the server",
                    "Server doesn't response", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            if (response != "Pong!")
            {
                MessageBox.Show("Server doesn't response. Please check the address of the server",
                    "Server doesn't response", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            response = Client.Sender("a\nRegister " + nameBox.Text);
            if (response != "True")
            {
                MessageBox.Show("We have already had such name!",
                    "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var a = new MainWindow(nameBox.Text, this);
            a.Show();
            this.Hide();
        }

        private void StartScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            Client.Close();
            this.Dispose();
            Application.Exit();
        }

        private void nameBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals((char)Keys.Enter)) Ok.PerformClick();
        }
    }
}
